<?php
/**
 * IG_Export_Txt
 *
 * @author maomao.thx@gmail.com
 * @copyright Copyright (c) 2009 Green Shore (http://www.oaiwgame.com)
 * @version $Id: Txt.php 621 2011-06-09 07:36:43Z lirong $
 */
Class IG_Export_Txt implements IG_Export_Interface
{
        /**
         * Lines to output in the excel document
         * @var array
         */
        private $_lines = array();

        /**
         * Used encoding
         * @var string
         */
        private $_sEncoding;

        /**
         * Convert variable types
         * @var boolean
         */
        private $_bConvertTypes;

        /**
         * Constructor
         * @param string $sEncoding Encoding to be used (defaults to UTF-8)
         * @param boolean $bConvertTypes Convert variables to field specification
         */
        public function __construct($sEncoding = 'UTF-8', $bConvertTypes = false)
        {
            $this->_bConvertTypes = $bConvertTypes;
            $this->setEncoding($sEncoding);
        }

        /**
         * Set encoding
         * @param string Encoding type to set
         */
        public function setEncoding($sEncoding)
        {
            $this->_sEncoding = $sEncoding;
        }

        /**
         * Add row
         *
         * Adds a single row to the document. If set to true, self::bConvertTypes
         * checks the type of variable and returns the specific field settings
         * for the cell.
         *
         * @param array $array One-dimensional array with row content
         */
        public function addRow($array, $sheet = null)
        {
            $cells = "";
            foreach ($array as $k => $v) {
                //$v = htmlentities($v, ENT_COMPAT, $this->sEncoding);
                $cells .= $v."\t";
            }
            $cells = trim($cells);
            $this->_lines[] = $cells . "\r\n";
        }

        /**
         * Add an array to the document
         * @param array 2-dimensional array
         */
        public function addRows($array, $sheet = null)
        {
            foreach ($array as $k => $v) {
                $this->addRow($v->toArray());
            }
        }

        /**
        * Send HTTP headers for the Txt file.
        *
        * @param string $filename The filename to use for HTTP headers
        * @access public
        */
        public function export($filename)
        {
            // correct/validate filename
            $filename = preg_replace('/[^aA-zZ0-9\_\-]/', '', $filename);

            header("Content-Type: application/octet-stream; charset=" . $this->sEncoding);
            header("Content-Disposition: inline; filename=\"" . $filename . ".txt\"");
            header("Expires: 0");
            header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
            header("Pragma: public");
            echo $this->getFileContent();
        }

        /**
         * 保存文件到指定目录
         * @param string $path
         * @param string $filename
         */
        public function save($path, $filename)
        {
            $fname = $filename.date("Y-m-d").".txt";
            //创建文件,$fname为文件名
            $of = fopen($path.$fname,'w');
            $content = $this->getFileContent();
            if ($of) {
                //把内容写入文件里
                fwrite($of,$content);
            } else {
                throw new IG_Exception("A document with this name already exists");
            }
            fclose($of);
        }
        /**
         * Generate the excel file
         * @param null
         */
        public function getFileContent()
        {
            foreach ($this->_lines as $line) {
                $content .= $line;
            }
            return trim($content);
        }
        /**
        * @desc 释放可用资源
        * @return null
        */
        public function release()
        {
            unset($this->_lines);
        }
        /**
         * 导入Txt
         * @param $filename
         * @return array
         */
        public function import($filename)
        {
            $fp = fopen($filename, "r");
            while (!feof($fp)) {
                $content = fgets($fp, filesize($filename));
                $arr[] = $content;
            }
            $_ra = array();
            foreach ($arr as $key=>$value) {
                $_ra[$key] = explode("\t",$value);
            }
            return $_ra;
        }
}